package rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

public class Spark68_Operate_foreach {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final List<Integer> nums = Arrays.asList(4,3,2,1);
        final JavaRDD<Integer> rdd = jsc.parallelize(nums,2);
        rdd.groupBy(
          num -> num % 2 == 0
        );

        //collect是单点循环，collect会把executor端的数据按照分区序列拉取会Drive端，然后在Driver端继续宁打印，所以是和传入的数据一样
        rdd.collect().forEach(System.out::println);
        //因为没有collect，数据在Executor端打印，因为executor是分布式的，不确定哪个分区数据先打印，所以和原来的数据顺序不一致,并且是按照分区进行输出
        rdd.foreach(
                num -> System.out.println(num)
        );
        jsc.close();
    }
}
